package kr.ltkblue;

import java.io.IOException;
import java.net.URISyntaxException;
import java.net.URL;
import java.security.GeneralSecurityException;

import com.google.api.client.googleapis.auth.oauth2.GoogleCredential;
import com.google.api.client.http.FileContent;
import com.google.api.client.http.HttpResponseException;
import com.google.api.client.http.HttpTransport;
import com.google.api.client.http.javanet.NetHttpTransport;
import com.google.api.client.json.jackson.JacksonFactory;
import com.google.api.services.drive.Drive;
import com.google.api.services.drive.DriveScopes;
import com.google.api.services.drive.model.File;

public class driveP12 {

	/** Email of the Service Account */
	private static final String SERVICE_ACCOUNT_EMAIL = "96944880731-micp715bqn19ciofnlga9ok4926veg46@developer.gserviceaccount.com";

	/** Path to the Service Account's Private Key file */
	private static final String SERVICE_ACCOUNT_PKCS12_FILE_PATH = "C:/Users/ltkblue/workspace/ltkblue/src/main/resource/dc728c029fae8c3649678c9b82f38b26719aec3e-privatekey.p12";

	/**
	 * Build and returns a Drive service object authorized with the service
	 * accounts.
	 *
	 * @return Drive service object that is ready to make requests.
	 */
	public static Drive getDriveService() throws GeneralSecurityException,
			IOException, URISyntaxException {
		HttpTransport httpTransport = new NetHttpTransport();
		JacksonFactory jsonFactory = new JacksonFactory();
		GoogleCredential credential = new GoogleCredential.Builder()
				.setTransport(httpTransport)
				.setJsonFactory(jsonFactory)
				.setServiceAccountId(SERVICE_ACCOUNT_EMAIL)
				.setServiceAccountScopes(DriveScopes.DRIVE)
				.setServiceAccountPrivateKeyFromP12File(
						new java.io.File(SERVICE_ACCOUNT_PKCS12_FILE_PATH))
				.build();
		Drive service = new Drive.Builder(httpTransport, jsonFactory, null)
				.setHttpRequestInitializer(credential).build();
		return service;
	}

	static void printFile(Drive service, String fileId) {
		try {
			File file = service.files().get(fileId).execute();

			System.out.println("Title: " + file.getTitle());
			System.out.println("Description: " + file.getDescription());
			System.out.println("MIME type: " + file.getMimeType());
		} catch (HttpResponseException e) {
			if (e.getStatusCode() == 401) {
				// Credentials have been revoked.
				// TODO: Redirect the user to the authorization URL.
				throw new UnsupportedOperationException();
			}
		} catch (IOException e) {
			System.out.println("An error occurred: " + e);
		}
	}

	static String insert(Drive service) {

		// Insert a file
		File body = new File();
		body.setTitle("My document");
		body.setDescription("A test document");
		body.setMimeType("text/plain");

		URL fileUrl = ClassLoader.getSystemResource("./");
		java.io.File baseDir = new java.io.File(fileUrl.getFile());

		java.io.File fileContent = new java.io.File(baseDir, "testFile.txt");
		FileContent mediaContent = new FileContent("text/plain", fileContent);
		File file=null;
		try {
			file = service.files().insert(body, mediaContent).execute();
			System.out.println("File ID: " + file.getId());
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return  file.getId();
	}

	public static void main(String[] args) {
		try {
			Drive service = getDriveService();
			String fileId = insert(service);
			printFile(service, fileId);
		} catch (GeneralSecurityException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (URISyntaxException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

	}
}
